<?php 
class activity
{
	var $db;
	function activity()
	{
		global $db;	
		$this -> db     = & $db;
	}
	//添加活动
	function add_activity()
	{
		$sql="INSERT INTO ".ACTIVITY." (`title` ,`desc` , `start_time` ,`end_time` ,`create_user` ,`status`,`displayorder`,`pic` ,`catid`,`brandid`,`discount`,`tags`,`type`,`goods_id`,`type1`,`guojia`,`guojiapic`)VALUES ( '$_POST[title]','$_POST[desc]','".strtotime($_POST['stime'])."','".strtotime($_POST['etime'])."','".$_SESSION[ADMIN_USER]."','$_POST[status]','0','$_POST[pic]','$_POST[catid]','$_POST[brandid]','$_POST[discount]','$_POST[tags]','$_POST[type]','$_POST[goods_id]','$_POST[type1]','$_POST[guojia]','$_POST[guojiapic]')";
		$this->db->query($sql);
	}
	//修改活动
	function edit_activity()
	{
		if(empty($_POST['brand'])){
			$_POST['brandid'] = 0;
		}
		$sql="UPDATE ".ACTIVITY."  SET `title` = '$_POST[title]',`desc` = '$_POST[desc]',`start_time` =  '".strtotime($_POST['stime'])."',`end_time` = '".strtotime($_POST['etime'])."',`pic` = '$_POST[pic]',`status` = '$_POST[status]' ,`catid`='$_POST[catid]',`brandid`='$_POST[brandid]',`discount`='$_POST[discount]' ,`tags`='$_POST[tags]',`type`='$_POST[type]',`goods_id`='$_POST[goods_id]',`type1`='$_POST[type1]',`guojia`='$_POST[guojia]',`guojiapic`='$_POST[guojiapic]'
		WHERE `id` ='$_POST[id]'  ";	
		$this->db->query($sql);
		
		$sql ="update ".ACTIVITYPRODUCT. " set `status`='$_POST[status]', `start_time` =  '".strtotime($_POST['stime'])."',`end_time` = '".strtotime($_POST['etime'])."' where activity_id= '$_POST[id]'";
		
		$this->db->query($sql);
		
	}
	//获取所有活动列表
	function get_activity_list()
	{
		$sql="select id,title,start_time,end_time from ".ACTIVITY." where status>0 ";
		$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$this->db->query($sql);
			$page->totalRows =$this->db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",20";
		//=====================
		$this->db->query($sql);
		$re['list']=$this->db->getRows();
		$re['page']=$page->prompt();	
		return $re;
	}
	
	function save_activity_product()
	{
		global $buid;
		$id=$_POST['id']*1;

		if(!empty($_POST['chk']))
		{
			foreach($_POST['chk'] as $val)
			{
				$sql="select id from ".ACTIVITYPRODUCT." where product_id='$val' and activity_id=$id";
				$this->db->query($sql);
				$aid=$this->db->fetchField('id');
				if(!$aid)
				{
					//如果此商品在关闭或过期的活动中，刚删除之前活动中的该商品
					$now_time = strtotime(date("Y-m-d H:i:s"));
					$sql = "select a.id from ".ACTIVITYPRODUCT." as a left join ".ACTIVITY." as b on b.id=a.activity_id where (b.status<=0 or b.end_time<'".$now_time."')";
					$this->db->query($sql);
					$pid=$this->db->fetchField('id');
					if($pid){
						$sql = "delete from ".ACTIVITYPRODUCT." where product_id='".$val."'";
						$this->db->query($sql);
					}
					$sql="select id,title,start_time,end_time,status from ".ACTIVITY." where id=$id ";
					$this->db->query($sql);
					$pinfo=$this->db->fetchRow();
					
					$sql="INSERT INTO ".ACTIVITYPRODUCT."(`activity_id` ,`product_id` ,`status` ,`create_user`,`start_time`,`end_time`) VALUES ('$id', '$val', '$pinfo[status]','".$_SESSION[ADMIN_USER]."','$pinfo[start_time]','$pinfo[end_time]')";
					$re=$this->db->query($sql);
				}
			}
		}
		return $re;
	}
	
	//（独立秒杀）
	/**
	 * 添加
	 * @param number $type 5为秒杀 10为促销
	 * @return number|mixed
	 */
	function add_activity_spike($type=5)
	{
	    if($_POST['stime'] != $_POST['etime'])
	    {
	        $sql="INSERT INTO ".ACTIVITYSPIKE." (`pid` ,`sku_id` , `sec_price` ,`start_time` ,`end_time` ,`create_user`,`sec_limit`,`sec_total`,`sec_stock`,`sec_true_stock`,`status`,`type`,`pic`  )VALUES ('".$_POST['pid']."','".$_POST['sku_id']."', '".$_POST['sec_price']."','".($_POST['stime'])."','".($_POST['etime'])."','".$_SESSION['ADMIN_USER']."',$_POST[sec_limit],$_POST[sec_total],$_POST[sec_true_stock],$_POST[sec_true_stock],$_POST[status],$type,'$_POST[pic]')";
	        $re=$this->db->query($sql);
	    }
	    return $re;
	    
	}
	//判断是否存在未过期的秒杀
	function get_activity_spike_buy_skuid($skuid,$type=5,$spike_id=NULL)
	{
	    $now_time = date("Y-m-d H:i:s");
	    $sql="SELECT 1 FROM ".ACTIVITYSPIKE ." where sku_id={$skuid} and status>0 and type={$type} and end_time>'{$now_time}'";
	    if ($spike_id){
	           $sql .=" and id !={$spike_id}";
	       }
	    $sql.=" limit 1";
	    $this->db->query($sql);
	    
	    $rs=$this->db->fetchRow();
	    
	    return $rs?true:false;
	}
	//判断秒杀的状态
	function get_activity_spike_buy_id($spike_id)
	{
	    $now_time = date("Y-m-d H:i:s");
	    $sql="SELECT 1 FROM ".ACTIVITYSPIKE ." where id={$spike_id} and status>0 and end_time>'{$now_time}' and start_time<'{$now_time}' limit 1";
	    $this->db->query($sql);
	    $rs=$this->db->fetchRow();
	    return $rs?true:false;
	}
	
	
	//编辑秒杀
	function edit_activity_spike(){
	    if($_POST['stime'] != $_POST['etime'])
	    {
	    $sql="update ".ACTIVITYSPIKE." set 
	        ".ACTIVITYSPIKE.".start_time='".($_POST['stime'])."',
	        ".ACTIVITYSPIKE.".end_time='".($_POST['etime'])."',
	        ".ACTIVITYSPIKE.".sec_price='$_POST[sec_price]',
	        ".ACTIVITYSPIKE.".sec_limit='$_POST[sec_limit]',
	        ".ACTIVITYSPIKE.".sec_stock='$_POST[sec_true_stock]',
	        ".ACTIVITYSPIKE.".sec_total='$_POST[sec_total]',
	        ".ACTIVITYSPIKE.".sec_true_stock='$_POST[sec_true_stock]',
	        ".ACTIVITYSPIKE.".pid='$_POST[pid]',
	        ".ACTIVITYSPIKE.".sku_id='$_POST[sku_id]',
	        ".ACTIVITYSPIKE.".status='$_POST[status]' ,
	        ".ACTIVITYSPIKE.".pic='$_POST[pic]'
   where  ".ACTIVITYSPIKE.".id='$_POST[id]'";
	     $re=$this->db->query($sql);
	    }
	    return $re;
	}
	
	//删除秒杀
	function del_activity_spike($spike_id){
	    
	     $sql="update ".ACTIVITYSPIKE." set 
	     
	         ".ACTIVITYSPIKE.".status=-9
	     
	      where  ".ACTIVITYSPIKE.".id='$spike_id'limit 1";
	     
	     $re=$this->db->query($sql);
	     return $re;
	    
	}
	
}
?>